package com.lp.multi.juc;

/**
 * @author: lp
 * @create: 2021-06-23 16:59
 * @description: TODO
 */
public class TicketTest {
    public static void main(String[] args) {
        TicketJuc ticket = new TicketJuc();
        new Thread(()->{
            for (int i = 0; i < 500; i++) {
                //模拟延时
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket.sale();
            } },"a").start();
        new Thread(()->{
            for (int i = 0; i < 500; i++) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket.sale();
            } },"b").start();
        new Thread(()->{
            for (int i = 0; i < 500; i++) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket.sale();
            } },"c").start();
    }

}


class Ticket{
    private int number =30;

    //synchronized  本质是队列+锁
    public synchronized void sale(){
        if(number >0){
            System.out.println(Thread.currentThread().getName() + "卖了第" + (number--) + "张票" + ",还剩余" + number + ("张票"));
        }
    }

}

